using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.Refunds;
using JlPay.Sdk.Models.Trans.Refunds;

namespace JlPay.Sdk.Examples.Trans.Refunds
{
    /// <summary>
    /// 交易 - 退款申请功能使用示例
    /// </summary>
    public class RefundsDemo
    {
        /// <summary>
        /// 构建退款申请请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static RefundsRequest BuildRefundsRequest()
        {
            var request = new RefundsRequest();
            
            // 设置请求参数
            request.MchId = "849584358120018";
            request.OutTradeNo = "20250318500005Test";
            request.OriOutTradeNo = "20250318100005Test";
            // request.OriTransactionId = "80011557201909171853011568";
            request.TotalFee = "1";
            request.Remark = "测试退款申请";
            request.MchCreateIp = "127.0.0.1";
            request.Longitude = "116.397128";
            request.Latitude = "39.916527";
            request.NotifyUrl = "http://172.20.20.85:38001/jlpay/refunds/notify";
            
            return request;
        }

        /// <summary>
        /// 退款申请示例
        /// </summary>
        public static async Task RefundsDemoAsync()
        {
            Console.WriteLine("开始执行退款申请示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var refundsService = new RefundsService(config);

            // 创建退款申请请求
            var request = BuildRefundsRequest();

            try
            {
                // 发送请求
                var response = await refundsService.RefundsAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("退款申请", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new RefundsResponse();
                response.HandleResponseWithException("退款申请", config, ex);
            }

            Console.WriteLine("退款申请示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 退款申请示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await RefundsDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 退款申请示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 